package cn.zyjblogs.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

public class NettyByteBuf02 {
    public static void main(String[] args) {
        //创建ByteBuf
        ByteBuf buf = Unpooled.copiedBuffer("hello,world", CharsetUtil.UTF_8);
        //使用相关的方法
        if (buf.hasArray()){
            byte[] content = buf.array();
            //将 content 转成字符串
            System.out.println(new String(content,CharsetUtil.UTF_8).trim());
            System.out.println("byteBuf= "+buf);
            //偏移量
            System.out.println("偏移量=" +buf.arrayOffset());
            //
            System.out.println("readerIndex="+buf.readerIndex());
            System.out.println("writerIndex="+buf.writerIndex());
            System.out.println("capacity="+buf.capacity());
            System.out.println("readableBytes  len="+buf.readableBytes());
            System.out.println(buf.readByte());
            System.out.println("readableBytes  len="+buf.readableBytes());
            System.out.println(buf.getByte(0));
            System.out.println("readableBytes  len="+buf.readableBytes());
            for (int i = 0; i < buf.readableBytes(); i++) {
                System.out.println((char)buf.getByte(i));
            }
            /**
             * index: 从那个索引位置开始读取
             * length: 读取的长度
             * 真正读取的范围 [index,index+length)
             */
            System.out.println(buf.getCharSequence(0,4,CharsetUtil.UTF_8));
            System.out.println(buf.getCharSequence(4,6,CharsetUtil.UTF_8));
        }
    }
}
